package com.feel.mall.goodsservice.service;

import com.feel.mall.common.bean.Category;
import com.feel.mall.common.bean.Goods;
import com.feel.mall.common.exception.FeelException;
import com.feel.mall.common.service.GoodsService;
import com.feel.mall.goodsservice.mapper.CategoryMapper;
import com.feel.mall.goodsservice.mapper.GoodsMapper;
import com.feel.mall.goodsservice.mapper.GoodsPicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: 刘永钦
 * @Description:
 */
@DubboService
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsPicMapper goodsPicMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(Goods goods) throws FeelException {
        // 判断分类是否存在,并且分类是否是子分类
        Category category = categoryMapper.selectById(goods.getCategoryId());
        if (category == null || category != null && category.getParentId().equals(0)) {
            throw new FeelException("分类不合法,请重新输入");
        }

        // 查找是否存在同名商品
        Goods condition = new Goods();
        condition.setCategoryId(goods.getCategoryId());
        condition.setName(goods.getName());
        condition.setColor(goods.getColor());
        condition.setVersion(goods.getVersion());

        // 上边查找对name是模糊查找,使用stream流对数据进行进一步判断
        if (goodsMapper.selectByCondition(condition)
                .stream()
                .anyMatch(item -> item.getName().equals(goods.getName()))) {
            throw new FeelException("存在相同商品无法添加");
        }

        // 添加商品
        goodsMapper.insert(goods);
        // 设置图片的商品id
        if (goods.getPicList() != null && goods.getPicList().size() > 0) {
            goods.getPicList()
                    .stream()
                    .forEach(item -> item.setGoodsId(goods.getId()));
            // 添加图片
            goodsPicMapper.insert(goods.getPicList());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) throws FeelException {
        // 判断商品是否存在
        if (goodsMapper.selectById(id) == null) {
            throw new FeelException("该商品不存在,无法删除");
        }

        // 判断是否有关联订单
        // todo

        // 删除商品图片
        goodsPicMapper.delete(id);
        // 删除商品
        goodsMapper.delete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Goods goods) throws FeelException {

        Goods newInfo = goods;
        Goods oldInfo = goodsMapper.selectById(goods.getId());
        // 判断商品是否存在
        if (oldInfo == null) {
            throw new FeelException("该商品不存在,无法修改");
        }

        // 判断是否修改了分类
        if (newInfo.getCategoryId() != null && !newInfo.getCategoryId().equals(oldInfo.getCategoryId())) {
            // 判断修改之后的分类是否存在并且必须为子分类
            Category category = categoryMapper.selectById(newInfo.getCategoryId());
            if (category == null || (category != null && category.getParentId().equals(0))) {
                throw new FeelException("修改之后分类不存在,或者不合规");
            }
        }

        // 不能同名 分类  名字  颜色  版本
        Integer newCategoryId = newInfo.getCategoryId() == null ? oldInfo.getCategoryId() : newInfo.getCategoryId();
        String newName = newInfo.getName() == null ? oldInfo.getName() : newInfo.getName();
        String newColor = newInfo.getColor() == null ? oldInfo.getColor() : newInfo.getColor();
        String newVersion = newInfo.getVersion() == null ? oldInfo.getVersion() : newInfo.getVersion();
        Goods condition = new Goods();
        condition.setCategoryId(newCategoryId);
        condition.setName(newName);
        condition.setColor(newColor);
        condition.setVersion(newVersion);

        if (goodsMapper.selectByCondition(condition)
                .stream()
                .anyMatch(item -> item.getName().equals(newInfo.getName()) && !item.getId().equals(newInfo.getId()))) {
            throw new FeelException("修改之后信息与其他商品相同无法修改");
        }

        // 修改商品信息
        goodsMapper.update(newInfo);

        // 修改图片
        // 删除之前图片
        goodsPicMapper.delete(newInfo.getId());
        if (newInfo.getPicList() != null && newInfo.getPicList().size() > 0) {
            // 设置每个图片商品id
            newInfo.getPicList()
                    .stream()
                    .forEach(pic -> pic.setGoodsId(newInfo.getId()));
            goodsPicMapper.insert(newInfo.getPicList());
        }
    }

    @Override
    public Goods selectById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public PageInfo<Goods> selectByCondition(Goods condition, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> list = goodsMapper.selectByCondition(condition);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<Goods> userSelectByCondition(Goods condition, Integer pageNum, Integer pageSize) {
        if (condition.getCategoryId() == null) {
            List<Integer> categoryList = categoryMapper.getchildIdList(condition.getParentId());
            condition.setCategoryList(categoryList);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Goods> list = goodsMapper.userSelectByCondition(condition);
        PageInfo<Goods> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Goods> selectByCategoryId(Integer id) {
        Goods condition = new Goods();
        condition.setCategoryId(id);
        condition.setStatus(1);
        condition.setRecom(1);
        List<Goods> list = goodsMapper.selectByCondition(condition);
        return list;
    }
}
